<template>
  <div style="padding:30px;">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input v-model="formInline.gdName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="商品分类">
        <el-select v-model="formInline.gdCategory" placeholder="请选择" @change="search">
          <el-option v-for="cate in category" :label="cate" :value="cate" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="resetSearchForm('searchForm')" icon="el-icon-refresh">清空</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="openAddDialog" icon="el-icon-plus">添加商品</el-button>
      </el-form-item>
    </el-form>

<!--    商品表格展示-->
    <el-table :data="tableGoods" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="gdId" label="商品编号" align="center"></el-table-column>
      <el-table-column prop="gdName" label="商品名称" align="center"></el-table-column>
      <el-table-column prop="gdCategory" label="商品类目" align="center"></el-table-column>
<!--      <el-table-column prop="gdType" label="商品类型" align="center"></el-table-column>-->
      <el-table-column prop="gdPrice" label="商品价格" align="center"></el-table-column>
      <el-table-column prop="gdUnit" label="商品单位" align="center"></el-table-column>
      <el-table-column prop="gdProperty" label="商品属性" align="center"></el-table-column>
      <el-table-column prop="gdSpec" label="规格" align="center"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import {getList,delById,delBatch,findCategory} from '@/api/nested';
import router from '@/router'
export default {
  name:'Menu2',
  data() {
    return {
      pageNum:1,
      pageSize: 5,
      currentPage:1,
      total:0,
      category:[],
      formInline: {
        gdName:'',
        //gdType:'',
        gdCategory:'',
      },
      tableGoods:[],
    }
  },
  methods: {
    fetchData() {
      //分页查询参数
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        gdName:this.formInline.gdName,
        gdCategory:this.formInline.gdCategory
      }

      //getList是api/unit/.js中的
      getList(params).then(res => {
        let pageInfo = res.data;
        // console.log("pageInfo",pageInfo)
        this.tableGoods = pageInfo.list;
        this.total = pageInfo.total;
      })
    },
    //商品分类搜索
    cateList(){
      findCategory().then(res => {
        //console.log(111)
        this.category = res.data;
        //console.log(this.category)
      })
    },
    //模糊搜索
    search(){
      this.fetchData()
    },

    //重置表单
    resetSearchForm(searchForm){
      this.formInline = {}
      this.fetchData()
    },
    //多选框
    handleSelectionChange(selectionRowArr) {
      this.ids = [];
      for (let i = 0; i < selectionRowArr.length; i++) {
        this.ids.push(selectionRowArr[i].gdId)
      }
    },
    // 页面大小改变时触发
    handleSizeChange(val) {
      this.pageSize = val; // 将当前页大小存起来，方便下一方法使用
      console.log(`每页 ${val} 条`);
      this.fetchData()
    },
    // 当前页码改变时触发
    handleCurrentChange(val) {
      this.pageNum = val;
      console.log(`当前页: ${val}`);
      this.fetchData()
    },
    //删除
    handleDelete(row){
      //console.log(row)
      this.$confirm('是否确认删除商品名称为"'+row.gdName+'"的商品？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          gdId:row.gdId
        }
        delById(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!',
            duration: 1000
          });
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    //商品信息数据更新修改
    handleUpdate(){
      router.push({name:'Menu1'})
    },

    //批量删除商品
    // deleteBatch(){
    //   this.$confirm('是否确认删除单位编号为['+this.ids+']的数据项？', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     let idStr = this.ids.join(",")
    //     let params = {
    //       ids:this.ids.join(",")
    //     }
    //     delBatch(params).then(res => {
    //       this.$message({
    //         type: 'success',
    //         message: '删除成功!',
    //         duration: 1000
    //       });
    //       this.fetchData();
    //     })
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '已取消删除'
    //     });
    //   });
    // },

    openAddDialog(){
      router.push({name:'Menu1'})
    }


  },
  //生命周期钩子函数
  created() {
    this.fetchData()
    this.cateList()
  },
}
</script>

<style scoped lang="scss">

</style>
